<?php
/*
 * 		author GuoLong 
 * 		2014/12/02
 * 		email:guolong@howdo.cc
 */
class VideoController extends Controller {
	
	//设置上传视频路径
	private $rootPath = "";		///var/www/html/howdo/
	private $targetPath = "common/upload/";
	private $uploadpath;
	private $send_mes;
	public function __construct() {
		parent::__construct ();
		$this->view->web_host = $this->getRequest ()->hostUrl;
		$this->view->web_app_url = $this->getRequest ()->hostUrl . "/clientapi.php";
		$this->view->web_domain_url = $this->getRequest ()->hostUrl . "/domain.php" ;
		$this->view->web_domain_aac = $this->getRequest ()->hostUrl . "/domainaac.php" ;
		$this->uploadpath = $this->rootPath.$this->targetPath;
		$this->send_mes=$this->getRequest()->hostUrl. "/common/clientapi/images/50.png";
	}
	//test
	public function Index(){
		$this->view->display("index.htm");
	}
	
	/*
	 * 根据视频id获取视频详情
	 * @author GuoLong
	* 2014/12/02
	*/
// 	http://localhost/xdfServer/clientapi.php/video/getvideoinfo/video_id/1
	public function getVideoInfo(){
		$video_id=$this->getRequest()->get("video_id");
		$plant=$this->getRequest()->get("plant");
		$video=new video();
		if($plant=="tea"){
		//	echo "test";
			$data=$video->teaGetVideoInfoById($video_id);
			unset($data["name"]);
	//		$data["student"]="test";
			$data["section"]=$data["section"]."/".$data["section_amount"];
			unset($data["section_amount"]);
		}else{
			$data=$video->getVideoInfoById($video_id);
			unset($data["name"]);
		}
		if($data){
			$this->view->setState ( "1" );
			$this->view->setStatus ( "1" );
			$this->view->setMsg ( "获取成功！" );
		}else{
			$data=null;
			$this->view->setStatus ( "1" );
			$this->view->setState ( "0" );
			$this->view->setMsg ( "没有更多了哟" );
		}
		$this->view->setData ( $data );
		$this->view->display ( "json" );
	}
	//
public function uploadVideo(){
		$msg="";
		$userinfo = $this->getData ( 'userinfo' );
		$user_id=$userinfo ['id'];
		
		$content_type=$this->view->getRequest()->get("content_type");
		$content_id=$this->view->getRequest()->get("content_id");
		$title=$this->getRequest()->get("title");
		$time=$this->getRequest()->get("time");
		$create_time=$this->getRequest()->get("create_time");
		$video_name=$this->getRequest()->get("video_name");
		$note_amount=$this->getRequest()->get("note_amount");
		$video=new video();
		//保存文件名称（不带后缀名的）到数据库
		//addvideo 
		if ($content_type == "1"){
			$type = VideoTypeEnum::VIDEO_ITEM;
		}else if($content_type == "2"){
			$type = VideoTypeEnum::VIDEO_PERIOD;
		}
		$video_id=$video->addVideo($user_id,$title,$video_name,$type,$time,$create_time);
		if(!$video_id){
			//$msg.="保存视频到数据库失败！";
			$msg.="上传失败了哟！";
			$this->view->setState("-4");
			$need_message_flag=false;
		}else{
			if($content_type == "1"){
				$item=new item();
				if($item->addItemVideo($content_id, $video_id)){
					//$msg.="视频与题目关联成功！";
					$msg.="上传成功了哟！";
					$this->view->setState("1");
					//检查是否有学生反馈该题目无视频
					//如果没有则设置无需推送
					$message=new message();
					$messageInfo=$message->getMessage(MessageCodeEnum::MESSAGE_TEA_NO_VIDEO, $content_id);
					if($messageInfo){
						//在此，设置
						//message
						$user = new user();
						$message_list = $this->getApp()->loadConf("message");
						$message = new message();
						$sender=$user_id;
						foreach ($messageInfo as $key=>$val){
							$messageInfo[$key]["phone"]=$user->getPhoneInfo($val['receiver']);
							$message->addMessage(MessageCodeEnum::MESSAGE_STU_NEW_VIDEO_ITEM,$video_id,
									MessageCodeEnum::getCodes(MessageCodeEnum::MESSAGE_STU_NEW_VIDEO_ITEM),$sender,$val["receiver"]);
						}
						//透传
						foreach($messageInfo as $key=>$val){
							$phone_info=$val["phone"];
							if ($phone_info['clientid']){
								$target = array(
										"type"=>$phone_info['type'],
										"ClientID"=>$phone_info['clientid'],
										"DeviceToken"=>$phone_info['devicetoken'],
								);
								$content = array(
										"info"=>"您的反馈过无视频的题目有了新的视频。",//透传
										"title"=>'UDO学生端',
										"text"=>"您的反馈过无视频的题目有了新的视频。",
										"logo_url"=>$this->send_mes,
								);
								$this->push->pushToSingle($target, $content);
							}
						}
					}
				}else{
					//$msg.="视频与题目关联失败！";
					$msg.="上传失败了哟！";
					$this->view->setState("-1");
				}
			}else{
	
				$note=new note();
				if($note_id=$note->addNote($video_name,$note_amount)){
					//$msg.="视频与笔记关联成功！";
					$msg.="上传成功了哟";
					$period=new period();
					if($period->addPeriodVideo($content_id, $video_id,$note_id)){
						//$msg.="视频与课程关联成功！";
						$this->view->setState("2");
						
						//在此，设置
						//message
						
						//获取这堂课的学生id
						$period=new period();
						$stuId=$period->getStudentIdByPeriod($content_id);
						
						foreach($stuId as $key=>$val){
							$messageInfo[$key]["receiver"]=$val["student_id"];
						}
						$user = new user();
						$message_list = $this->getApp()->loadConf("message");
						$message = new message();
						$sender=$user_id;
						foreach ($messageInfo as $key=>$val){
							$messageInfo[$key]["phone"]=$user->getPhoneInfo($val['receiver']);
							$message->addMessage(MessageCodeEnum::MESSAGE_STU_NEW_VIDEO_PERIOD,$video_id,
									MessageCodeEnum::getCodes(MessageCodeEnum::MESSAGE_STU_NEW_VIDEO_PERIOD),$sender,$val["receiver"]);
						}  
						//透传
						foreach($messageInfo as $key=>$val){
							$phone_info=$val["phone"];
							if ($phone_info['clientid']){
								$target = array(
										"type"=>$phone_info['type'],
										"ClientID"=>$phone_info['clientid'],
										"DeviceToken"=>$phone_info['devicetoken'],
								);
								$content = array(
										"info"=>"您有了新的课程视频。",//透传
										"title"=>'UDO学生端',
										"text"=>"您有了新的课程视频。",
										"logo_url"=>$this->send_mes,
								);
								$this->push->pushToSingle($target, $content);
							}
						}
					}else{
						//$msg.="视频与课程关联失败！";
						$msg.="上传出错了哟!";
						$need_message_flag=false;
						$this->view->setState("-2");
					}
				}else{
					//$msg.="视频与笔记关联失败！";
					$msg.="上传出错了哟";
					$need_message_flag=false;
					$this->view->setState("-3");
				}
			}
			
		}
		$this->view->setMsg($msg);
		$this->view->setData ( null );
		$this->view->display ( "json" );
	}
	function Play(){
		$msg="";
		$userinfo = $this->getData ( 'userinfo' );
		$user_id=$userinfo ['id'];
		$video_id = $this->getRequest ()->get ( 'video_id' );
		$video_title=$this->getRequest ()->get ( 'video_title' );
		
		$play_ok_flag=true;
// 		在这验证参数，以及权限问题
		
// 		$video = new video();
// 		$list = $video->getVideoNameById ( $video_id );
// 		if(!$list){
// 			$play_ok_flag=false;
// 		}
		if($play_ok_flag){
			$msg.="ok!";
			$this->view->setState ( "1" );
			$data["video_url"]=$this->view->web_app_url."/Video/Playvideo/video_id/".$video_id."/info/".$video_title;
			//记录用户观看行为
			$log=new log();
			$c_str=$this->getRequest()->cName;
			$a_str=$this->getRequest()->aName;
			$resource_type=$log->resource_type["video"];
			$resource_id=$video_id;
			$content="1";
			$state="1";
			$msg="观看视频,".$video_title;
			$video = new video();
				//视频浏览量增加 1
			$video->viewVideo($video_id);
			
			//记录用户行为
			if($log->addLog($user_id,$c_str,$a_str,$resource_type,$resource_id,$content,$state,$msg)){
				
			}else{ 
				//记录用户行为失败
				$log->addLog("0",$c_str,$a_str,$resource_type,$resource_id,$content,$state,$msg);
			}
		}else{
			$msg.="暂无权限噢";
			$this->view->setState ( "0" );
			$data=null;
		}
		$this->view->setMsg ($msg);
		$this->view->setData($data);
		$this->view->setStatus ( "1" );
		$this->view->display ( "json" );
	}
	//视频播放
	//http://119.254.108.124:10023/clientapi.php/video/playvideo/video_id/1
	function Playvideo(){
		$vid = $this->getRequest ()->get ( 'video_id' );
		($item_info = urldecode($this->getRequest ()->get ( 'info' )))||($item_info = urldecode("播放中"));
		if (!$vid){
			$this->view->setState ( "0" );
			$this->view->setStatus ( "1" );
			$this->view->setMsg ( "参数缺失" );
			$this->view->display ( "json" );
			return;
		}
		$userinfo = $this->getData ( 'userinfo' );
		if ($userinfo){
			$user_id=$userinfo ['id'];
			$video_id=$vid;
			$video_title=$item_info;
			//记录用户观看行为
			$log=new log();
			$c_str=$this->getRequest()->cName;
			$a_str=$this->getRequest()->aName;
			$resource_type=$log->resource_type["video"];
			$resource_id=$video_id;
			$content="1";
			$state="1";
			$msg="观看视频,".$video_title;
			//记录用户行为
			if($log->addLog($user_id,$c_str,$a_str,$resource_type,$resource_id,$content,$state,$msg)){
			
			}else{
				//记录用户行为失败
				$log->addLog("0",$c_str,$a_str,$resource_type,$resource_id,$content,$state,$msg);
			}
		}
		$video = new video();
		$list = $video->getVideoNameById ( $vid );
		if ($list){
			//-------------------------
			//视频浏览量增加 1
			//$video->viewVideo($vid);
			$qiniu_path=$this->_app->getData("qiniu_path");
			$video_name = $qiniu_path["qiniu_path_video"].$list['name'];
			$this->getView ()->video_name = $video_name;
			$this->getView ()->video_time = $list['time'];
			$this->getView ()->item_info =  str_replace("_", "/", $item_info);;
			$this->getView ()->display ( "player.html" );
		
		}
	}
	
	//获取笔记url
	function getNoteURL(){
		$userinfo = $this->getData ( 'userinfo' );
		$user_id=$userinfo ['id'];
		$note_id=$this->view->getRequest()->get("note_id");
		$note=new note();
		$qiniu_path=$this->getData("qiniu_path");
		if($note_info=$note->getNoteById($note_id)){
			$data["url"]=$qiniu_path["qiniu_path_video"].$note_info["name"]."/resource/note/";
			$data["amount"]=$note_info["amount"];
			$data["thumb_str"]="?imageView2/1/w/100/h/100";
			$this->view->setState("1");
			//$this->view->setMsg($note_id."success");
			$this->view->setMsg("获取成功");
		}else{
			$data=null;
			$this->view->setState("0");
			$this->view->setMsg("获取失败");
		}
		$this->view->setData ($data);
		$this->view->display ( "json" );
	}
	function teaGetNoteURL(){
		$userinfo = $this->getData ( 'userinfo' );
		$user_id=$userinfo ['id'];
		$note_id=$this->view->getRequest()->get("note_id");
		$note=new note();
		$qiniu_path=$this->getData("qiniu_path");
		if($note_info=$note->getNoteById($note_id)){
			$data["url"]=$qiniu_path["qiniu_path_video"].$note_info["name"]."/note.zip";
			$data["amount"]=$note_info["amount"];
			$this->view->setState("1");
			//$this->view->setMsg($note_id."success");
			$this->view->setMsg("获取成功");
		}else{
			$data=null;
			$this->view->setState("0");
			$this->view->setMsg("");
		}
		$this->view->setData ($data);
		$this->view->display ( "json" );
	}
}

